
/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           12/29/2007  Version:        2.0
///
/// ===========================================================================

function switchFooter(obj, name, totalResults) {
    try {
        var table = getFacetTableByObj(obj, name);
        if (table) {
            var re = /DLMIN.gif/;
            var img = obj.children[0];
            var expanded = re.test(img.src); 
            if (expanded){ // then collapse
                img.src = img.src.replace(/DLMIN.gif/, "DLMAX.gif");
            } else {
                img.src = img.src.replace(/DLMAX.gif/, "DLMIN.gif");
            }   
            
            setFacetTable(table, expanded, totalResults);
        }
    } catch (e) {}
    finally {return false;}
}

function setFacetTable(table, expanded, maxResults){
    var total = table.rows.length;     
    if (expanded){// then collapse and show last row with ...
        table.rows[total - 1].style.display = "block";  
    } else {
        table.rows[total - 1].style.display = "none";  
    }   

    for(var i = parseInt(maxResults) + 1; i<total-1; i++) {
        var row = table.rows[i];
        if (expanded){// then collapse and hide all rows
            row.style.display = "none";
        } else {
            row.style.display = "block";
        }
    }
}

function getFacetTableByObj(obj, name) {
    var parent = obj.parentElement;
    while ((parent != null) && (parent.tagName != "Table")) {
        parent = parent.parentElement;
        if (parent.getAttribute("facet") == name){
            return parent;
        }
    }
}

function getFacetTableByName(name) {
    var tables = document.body.getElementsByTagName("Table");
    for (var i=0; i<tables.length; i++) {
        var table = tables[i];
        if (table.getAttribute("facet") == name){
            return table;
        }
    }
}

function processFacetTables() {
    var tables = document.body.getElementsByTagName("Table");
    for (var i=0; i<tables.length; i++) {
        var table = tables[i];
        if (table.getAttribute("facet")){
            var count = table.getAttribute("count");
            var totalresults = table.getAttribute("totalresults");  
            setFacetTable(table, true, totalresults);
        }
    }
}
function initFacetTables(){
    try {
        if (arFacetTables != undefined){
            for (var i=0; i<arFacetTables.length; i++) {
                var name = arFacetTables[i];
                var totalResults = arTotalResults[i];
                var table = getFacetTableByName(name);
                setFacetTable(table, true, totalResults)
            }
        }
    } catch(e) {

    }
}

//*********** 2nd call ****************//  

function setProgress(url) {
    var images = document.images;
    for (var i=0; i<images.length; i++) {
        var img = images[i];
        if (img.progress != null) {
            img.src = url;
        }
    }
} 

function switchElement(id, toShow)
{
	var el=document.getElementById(id);
	if (el !=null) {
	    if (toShow) {
	        el.style.display = "inline";
	    } else {
	        el.style.display = "none";
	    }
	}
}


function setElAttribute(id, attName, attValue){
    var el=document.getElementById(id);
	el[attName] = attValue;
}

function addMatch(facetName, facetDisplayName, qs) {
    var match = prompt('Add additional match for ' + facetDisplayName, '[type the value]');
    if (!match) return false;
    var url = qs + ' ' + facetName + ':"' + encodeURI(match) + '"';
    GoToPageRelative(url);
}
